package com.ljw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljw.dto.BookDto;
import com.ljw.dto.GetFreeRoomDto;
import com.ljw.entity.Book;
import com.ljw.entity.Booksum;
import com.ljw.entity.Room;
import com.ljw.entity.User;
import com.ljw.mapper.BookMapper;
import com.ljw.service.BookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljw.service.BooksumService;
import com.ljw.service.RoomService;
import com.ljw.service.UserService;
import com.ljw.util.MyDateUtils;
import com.ljw.util.UUIDUtils;
import com.ljw.vo.BookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 预订信息表 服务实现类
 * </p>
 *
 * @author ljw
 * @since 2020-07-25
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private BooksumService booksumService;

    @Override
    public Page<BookVo> findAll(Page<BookVo> page, int userId) {
        return bookMapper.findAll(page,userId);
    }

    @Override
    public int book(BookDto bookDto) {
        //user是否存在
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_phone",bookDto.getUserPhone());
        User user = userService.getOne(userQueryWrapper);
        if(user == null)
            return 0;
        //是否空闲
        int roomId = bookDto.getRoomId();
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("room_type_id").eq("room_id",roomId);
        Room room = roomService.getOne(queryWrapper);
        GetFreeRoomDto getFreeRoomDto = new GetFreeRoomDto();
        getFreeRoomDto.setInDate(bookDto.getBookInDate());
        getFreeRoomDto.setOutDate(bookDto.getBookOutDate());
        getFreeRoomDto.setRoomTypeId(room.getRoomTypeId());
        List<Room> roomList = roomService.getFreeRoom(getFreeRoomDto);
        boolean f = false;
        for(Room room1:roomList){
            if(room1.getRoomId() == roomId) {
                f = true;
                break;
            }
        }
        if(!f) return -1;
        //生成订单号
        String orderNum = UUIDUtils.getOrderIdByUUID();
        //insert into book
        Book book = new Book();
        book.setBookInDate(MyDateUtils.stringToDate(bookDto.getBookInDate()));
        book.setBookOutDate(MyDateUtils.stringToDate(bookDto.getBookOutDate()));
        book.setBookRoomId(roomId);
        book.setBookUserId(user.getUserId());
        book.setBookOrderNum(orderNum);
        book.setBookSumPrice(bookDto.getBookSumPrice());
        save(book);
        //insert into booksum
        Date date = new Date();
        Booksum booksum = new Booksum();
        booksum.setBooksumCreateBy(bookDto.getCreateBy());
        booksum.setBooksumCreateTime(date);
        booksum.setBooksumInDate(MyDateUtils.stringToDate(bookDto.getBookInDate()));
        booksum.setBooksumOutDate(MyDateUtils.stringToDate(bookDto.getBookOutDate()));
        booksum.setBooksumRoomId(bookDto.getRoomId());
        booksum.setBooksumType("预订");
        booksum.setBooksumOrderNum(orderNum);
        booksum.setBooksumUserId(user.getUserId());
        booksum.setBooksumSumPrice(bookDto.getBookSumPrice());
        booksumService.save(booksum);
        return book.getBookId();
    }
}
